// haojk/pages/setSubbranch/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        applyButtonText: "立即申请",
        canApply: false,
        canInput: false,
        name: '',
        applyremark: '',
        canInput:false,
        jduniacid:'',
        QQ: '',
        weixin: '',
        mobile: '',
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var $that = this;


        //查看用户申请状态
        $that.getMyApplayPartner();
      
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var $that = this;
        var memberinfo = wx.getStorageSync('memberinfo');
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {


    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    confirm: function () {
        var $that = this;
        var memberinfo = wx.getStorageSync('memberinfo');
        if ($that.data.canApply) {
            if ($that.data.name == '') {
                wx.showModal({
                    title: '',
                    content: '姓名不能为空',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
                return false;
            } else if ($that.data.jduniacid == '') {

                wx.showModal({
                    title: '',
                    content: '京东联盟id不能为空',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
                return false;
            } else if ($that.data.mobile == '') {

                wx.showModal({
                    title: '',
                    content: '手机号码不能为空',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
                return false;
            }  else if ($that.data.applyremark == '') {

                wx.showModal({
                    title: '',
                    content: '申请理由不能为空',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
                return false;
            }
            if ($that.data.name==undefined){
                $that.setData({"name":""});
            }
            if ($that.data.jduniacid == undefined) {
                $that.setData({ "jduniacid": "" });
            }
            if ($that.data.QQ == undefined) {
                $that.setData({ "QQ": "" });
            }
            if ($that.data.mobile == undefined) {
                $that.setData({ "mobile": "" });
            }
            if ($that.data.weixin == undefined) {
                $that.setData({ "weixin": "" });
            }
            if ($that.data.applyremark == undefined) {
                $that.setData({ "applyremark": "" });
            }
            console.log($that.data);

            app.util.request({
                url: 'entry/wxapp/ApplayPartner',
                showLoading: true,
                data: {
                    "m": 'nets_haojk',
                    // "from_uid": e.data
                    "from_uid": "0",
                    name: $that.data.name,
                    applyremark: $that.data.applyremark,
                    jduniacid:$that.data.jduniacid,
                    QQ: $that.data.QQ,
                    mobile: $that.data.mobile,
                    weixin: $that.data.weixin,
                    memberid: memberinfo.memberid,
                },
                method: 'post',
                success: function (response) {
                    console.log(response);
                    if (response.data.errno == 0) {
                        $that.setData({ applyButtonText: "等待审核中" })
                        wx.showModal({
                            title: '提示',
                            content: response.data.message,
                            success: function (res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        })
                    }
                }
            });
        }

    },
  
    shopnameChange: function (e) {
        console.log(e.detail);
        this.setData({
            name: e.detail.value
        })
    },
    shopQQChange: function (e) {
        console.log(e.detail);
        this.setData({
            QQ: e.detail.value
        })
    },
    shopMobileChange: function (e) {
        console.log(e.detail);
        this.setData({
            mobile: e.detail.value
        })
    },
    shopweixingChange: function (e) {
        console.log(e.detail);
        this.setData({
            weixin: e.detail.value
        })
    },
    shopjduniacidChange:function(e){
        console.log(e.detail);
        this.setData({
            jduniacid: e.detail.value
        })
    },
    shopdescChange: function (e) {
        console.log(e.detail);
        this.setData({
            applyremark: e.detail.value
        })
    },


    //查询我的申请合伙人状态
    getMyApplayPartner: function () {
        var memberinfo = wx.getStorageSync('memberinfo');
        var $that = this;
        app.util.request({
            url: 'entry/wxapp/GetMyApplayPartner',
            data: {
                "m": 'nets_haojk',
                 memberid: memberinfo.memberid,
            },
            method: 'post',
            success: function (response) {
                console.log(response);
                console.log(response.data.errno);
                var obj = response.data;
              
                if (obj.errno == 0) {
                    console.log(response);

                    if (response.data.data) {
                        console.log("已经申请 过");

                        //页面赋值
                        if (obj.data.QQ != undefined) {
                            $that.setData({
                                QQ: obj.data.QQ,
                            })
                        }
                        if (obj.data.weixin != undefined) {
                            $that.setData({
                                weixin: obj.data.weixin,
                            })
                        }
                        $that.setData({
                            //  QQ: obj.data.QQ,
                            mobile: obj.data.mobile,
                            // weixin: obj.data.weixin,
                            name: obj.data.name,
                            applyremark: obj.data.applyremark,
                            jduniacid: obj.data.jduniacid

                        })
                        //已經申請過
                       $that.setData({ canInput: true });

                        $that.setData({applyButtonText:"等待审核中"})

                    } else {
                        //申请不成功可以执行立即申请
                        console.log("未申请过");
                        $that.setData({ canApply: true });
                    }
                }

            }
        });

    },


})